package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.admin.dto.LoginDto;
import com.heima.admin.entity.AdUser;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2021-12-23
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    /**
     * 保存自媒体用户
     * @param user
     * @return
     */
    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser user) {

        //根据APP端用户ID查询 自媒体用户记录是否存在
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getApUserId, user.getApUserId());
        WmUser one = this.getOne(queryWrapper);

        if (one != null) {
            //说明已经存在
            return ResponseResult.okResult(one);
        }

        //不存在直接保存
        //返回对象的原因是：因为还用通过userId进行article数据库的保存
        this.save(user);
        return ResponseResult.okResult(user);
    }


    /**
     * 自媒体用户登录
     * @param loginDto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto loginDto) {

        //1.判断是否有值
        if (loginDto == null || StringUtils.isEmpty(loginDto.getName()) || StringUtils.isEmpty(loginDto.getPassword())) {
            //返回缺少参数的结果
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.根据账号查询，是否存在该用户
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getName, loginDto.getName());
        WmUser one = this.getOne(queryWrapper);
        //判断one是否为空
        if (one == null) {
            //返回账号错误的结果
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //3.再根据密码进行查询进行判断，密码需要经过处理
        String pwdStr = loginDto.getPassword() + one.getSalt();
        String userEnterPwdEncodedStr = DigestUtils.md5DigestAsHex(pwdStr.getBytes());

        //判断密码是否正确
        if (!StringUtils.equals(userEnterPwdEncodedStr, one.getPassword())) {
            //返回密码错误的结果
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);

        }
        //4.制作token,需要进行脱敏操作
        HashMap<String, Object> claimMaps = new HashMap<>();
        claimMaps.put("userId", one.getId());
        claimMaps.put("userName", one.getName());
        String token = AppJwtUtil.getToken(claimMaps);

        //5.返回结果,需要进行脱敏处理
        one.setPhone(null);
        one.setSalt(null);
        one.setPassword(null);

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("user", one);
        resultMap.put("token", token);
        return ResponseResult.okResult(resultMap);
    }
}
